﻿/*****************************************************************************
 * Hyldahl.Services.TaskProcessing.Test            Created by: MSH 2008.07.22
 * --------------------------------------------------------------------------
 * FileName: Dummy\ErrorDummyTaskDataProvider.cs
 * --------------------------------------------------------------------------
 * Copyright 2009 Martin Sixhøj Hyldahl
 *****************************************************************************/

using System;
using Hyldahl.Services.TaskProcessing.Data;

namespace Hyldahl.Services.TaskProcessing.Test.Dummy
{
    public class ErrorDummyTaskDataProvider : TaskDataProvider
    {
        /*****************************************************
         * FIELDS
         *****************************************************/
        private TaskDataProviderError errorFlags;
        private bool saveTaskResultCalled;

        /*****************************************************
         * CONSTRUCTOR
         *****************************************************/

        /// <summary>
        /// Initializes a new instance of the <see cref="ErrorDummyTaskDataProvider"/> class.
        /// </summary>
        /// <param name="errorFlags">The error flags.</param>
        public ErrorDummyTaskDataProvider(TaskDataProviderError errorFlags)
        {
            this.errorFlags = errorFlags;
        }

        /*****************************************************
         * METHODS
         *****************************************************/

        /// <summary>
        /// Creates the task.
        /// </summary>
        /// <param name="task">The task.</param>
        public override void CreateTask(Task task)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Saves the task result.
        /// </summary>
        /// <param name="task">The task.</param>
        public override void SaveTaskResult(Task task)
        {
            this.saveTaskResultCalled = true;

            if ((this.errorFlags & TaskDataProviderError.SaveTaskResultError) == TaskDataProviderError.SaveTaskResultError)
                throw new InvalidOperationException();
        }

        /// <summary>
        /// Gets the tasks.
        /// </summary>
        /// <returns></returns>
        public override System.Collections.ObjectModel.Collection<Task> GetTasks()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets the task.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public override Task GetTask(Guid id)
        {
            return null;
        }

        /// <summary>
        /// Gets the an unprocessed task.
        /// </summary>
        /// <returns></returns>
        public override Task GetUnProcessedTask()
        {
            if ((this.errorFlags & TaskDataProviderError.GetUnProcessedTaskError) == TaskDataProviderError.GetUnProcessedTaskError)
                throw new InvalidOperationException();

            Task task = new Task();
            task.Id             = Guid.NewGuid();
            task.CreatedDate    = DateTime.UtcNow;

            if ((this.errorFlags & TaskDataProviderError.UnknownTaskType) == TaskDataProviderError.UnknownTaskType)
                task.TaskType = "Unknown Task Type.";
            else
                task.TaskType = "Fibonacci";

            if ((this.errorFlags & TaskDataProviderError.InvalidFibonaciTaskData) == TaskDataProviderError.InvalidFibonaciTaskData)
                task.TaskType = "Not a number";
            else
                task.TaskType = "10";

            return task;
        }

        /*****************************************************
         * PROPERTIES
         *****************************************************/

        /// <summary>
        /// Gets a value indicating whether [save task result called].
        /// </summary>
        /// <value>
        /// 	<c>true</c> if [save task result called]; otherwise, <c>false</c>.
        /// </value>
        public bool SaveTaskResultCalled
        {
            get { return saveTaskResultCalled; }
        }
    }
}
